package com.jiuzhou.guanwang.jzcp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jiuzhou.guanwang.jzcp.R;
import com.jiuzhou.guanwang.jzcp.adapter.OpenDetailAdapter;
import com.jiuzhou.guanwang.jzcp.adapter.OpenNumberAdapter;
import com.jiuzhou.guanwang.jzcp.adapter.OpenNumberAdapter1;
import com.jiuzhou.guanwang.jzcp.base.BaseActivity;
import com.jiuzhou.guanwang.jzcp.bean.NearlyLotteryResult;
import com.jiuzhou.guanwang.jzcp.presenter.LastOpenPresenter;
import com.jiuzhou.guanwang.jzcp.utils.CommonUtils;
import com.jiuzhou.guanwang.jzcp.view.LastOpenView;

import java.text.NumberFormat;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LastOpenActivity extends BaseActivity<LastOpenPresenter> implements LastOpenView {
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.iv_icon)
    ImageView ivIcon;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_expect)
    TextView tvExpect;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.rv_number)
    RecyclerView rvNumber;
    @BindView(R.id.rv_num)
    RelativeLayout rvNum;
    @BindView(R.id.tv_sale)
    TextView tvSale;
    @BindView(R.id.tv_price_pool)
    TextView tvPricePool;
    @BindView(R.id.ll_pool)
    LinearLayout llPool;
    @BindView(R.id.lv_detail)
    ListView lvDetail;
    private String mCode;
    private String mName;
    private OpenDetailAdapter mOpenDetailAdapter;
    private OpenNumberAdapter mOpenNumberAdapter;
    private OpenNumberAdapter1 mOpenNumberAdapter1;

    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding(R.layout.activity_last_open);
        ButterKnife.bind(this);
        Window window = getWindow();
//        if (VERSION.SDK_INT >= 19) {
//            window.addFlags(67108864);
//            if (VERSION.SDK_INT >= 21) {
//                window.getDecorView().setSystemUiVisibility(1280);
//                window.addFlags(Integer.MIN_VALUE);
//                window.setStatusBarColor(0);
//            }
//        }
        Intent intent = getIntent();
        this.mName = intent.getStringExtra("name");
        this.mCode = intent.getStringExtra("code");

        if ("超级大乐透".equals(this.mName)) {
            this.mName = "大乐透";
        }
        if ("任选9".equals(this.mName)) {
            this.mName = "任选九";
        }
        if ("福彩3d".equals(this.mName)) {
            this.mName = "3D";
        }
        initView();
        initData();
    }

    @Override
    protected LastOpenPresenter initPresenter() {
        return new LastOpenPresenter(this);
    }


    @Override
    protected void initialize() {

    }

    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        this.mName = intent.getStringExtra("name");
        if ("超级大乐透".equals(this.mName)) {
            this.mName = "大乐透";
        }
        if ("任选9".equals(this.mName)) {
            this.mName = "任选九";
        }
        if ("福彩3d".equals(this.mName)) {
            this.mName = "3D";
        }
    }

    private void initView() {
        rvNumber.setLayoutManager(new LinearLayoutManager(this, 0, false));

        switch (mCode){
            case "ssq":
                Glide.with(mContext)
                        .load(R.drawable.ic_ssq3)
                        .crossFade()
                        .into(ivIcon);
                break;
            case "dlt":
                Glide.with(mContext)
                        .load(R.drawable.ic_dlt3)
                        .crossFade()
                        .into(ivIcon);
                break;
            case "fc3d":
                Glide.with(mContext)
                        .load(R.drawable.ic_3d2)
                        .crossFade()
                        .into(ivIcon);
                break;
            case "pl3":
                Glide.with(mContext)
                        .load(R.drawable.ic_pls3)
                        .crossFade()
                        .into(ivIcon);
                break;
            case "pl5":
                Glide.with(mContext)
                        .load(R.drawable.ic_plw3)
                        .crossFade()
                        .into(ivIcon);
                break;
            case "qxc":
                Glide.with(mContext)
                        .load(R.drawable.ic_qxc3)
                        .crossFade()
                        .into(ivIcon);
                break;
            case "qlc":
                Glide.with(mContext)
                        .load(R.drawable.ic_qlc3)
                        .crossFade()
                        .into(ivIcon);
                break;
            case "js11x5":
                Glide.with(mContext)
                        .load(R.drawable.ic_11x5)
                        .crossFade()
                        .into(ivIcon);
                break;
            case "jsk3":
                Glide.with(mContext)
                        .load(R.drawable.ic_k3)
                        .crossFade()
                        .into(ivIcon);
                break;
        }
    }

    private void initData() {
        String str = this.mName;
        title.setText(mName);
        if (this.mOpenDetailAdapter == null) {
            this.mOpenDetailAdapter = new OpenDetailAdapter(this);
        }
        if (this.mOpenNumberAdapter == null) {
            this.mOpenNumberAdapter = new OpenNumberAdapter(this);
        }
        if (this.mOpenNumberAdapter1 == null) {
            this.mOpenNumberAdapter1 = new OpenNumberAdapter1(this);
        }

        presenter.getData(str);

    }

    @Override
    public void dismissLoadingDialog() {
        getLoadingProgressDialog().dismiss();
    }

    @Override
    public void showLoadingDialog() {
        getLoadingProgressDialog().show();
    }

    @Override
    public void onDataSuccess(NearlyLotteryResult result) {
        if (result != null && "200".equals(result.retCode)) {
            if (!(result.result == null || CommonUtils.isEmpty(result.result.lotteryDetails) || CommonUtils.isEmpty(result.result.lotteryNumber) || TextUtils.isEmpty(result.result.name))) {
                tvTitle.setText(result.result.name);
                if ("双色球".equals(result.result.name)) {
                    result.result.lotteryNumber.set(result.result.lotteryNumber.size() - 1, ((String) result.result.lotteryNumber.get(result.result.lotteryNumber.size() - 1)) + "特别");
                }
                if ("七乐彩".equals(result.result.name)) {
                    result.result.lotteryNumber.set(result.result.lotteryNumber.size() - 1, ((String) result.result.lotteryNumber.get(result.result.lotteryNumber.size() - 1)) + "特别");
                }
                if ("大乐透".equals(result.result.name)) {
                    result.result.lotteryNumber.set(result.result.lotteryNumber.size() - 2, ((String) result.result.lotteryNumber.get(result.result.lotteryNumber.size() - 2)) + "特别");
                    result.result.lotteryNumber.set(result.result.lotteryNumber.size() - 1, ((String) result.result.lotteryNumber.get(result.result.lotteryNumber.size() - 1)) + "特别");
                }
            }
            if (!TextUtils.isEmpty(result.result.awardDateTime) && result.result.awardDateTime.contains(" ")) {
                tvTitle.setText(result.result.awardDateTime.substring(0, result.result.awardDateTime.indexOf(" ")));
            }
            if (!TextUtils.isEmpty(result.result.period)) {
                tvExpect.setText(getResources().getString(R.string.expect, new Object[]{result.result.period}));
            }
            NumberFormat nf1 = NumberFormat.getInstance();
            if (result.result.sales == -1.0d) {
                tvSale.setText("暂无数据");
            }
            tvSale.setText(nf1.format(result.result.sales));
            if ("0".equals(nf1.format(result.result.pool))) {
                llPool.setVisibility(View.GONE);
            } else {
                llPool.setVisibility(View.VISIBLE);
            }
            tvPricePool.setText(nf1.format(result.result.pool));
            if (mName.contains("胜负彩") || mName.contains("任选九")) {
                rvNumber.setAdapter(mOpenNumberAdapter1);
                mOpenNumberAdapter1.setData(result.result.lotteryNumber);
            } else {
                rvNumber.setAdapter(mOpenNumberAdapter);
                mOpenNumberAdapter.setData(result.result.lotteryNumber);
            }
            lvDetail.setAdapter(mOpenDetailAdapter);
            mOpenDetailAdapter.setData(result.result.lotteryDetails);
        }
    }

    @OnClick(R.id.tv_title_right)
    public void onViewClicked() {
        Intent intent = new Intent(mContext, PastOpenActivity.class);
        intent.putExtra("name", mName);
        intent.putExtra("code",mCode);
     startActivity(intent);
    }
}